package com.activenetwork.smlite.testcase.events.createEvents.basics;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.RandomString;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics005 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics005.class);
	
	@Test
	public void Verify_add_new_location_works_correctly(){
		
		loginBusiness.login();
		
		String locationName = "China " + RandomString.generateRandomString(5);
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		
		/**
		 * Try to find the non-existing address
		 */
		EventBasicType.Location location1 = new EventBasicType.Location();
		location1.setLocationName(locationName);
		try{
			eventBasicSubview = eventBasicSubview.selectEventLocationAJAX(location1);
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics005.class.getName(), 
					"Verify adding non-existing address", 
					locationName + " is existing!"));
		}catch(RuntimeException e){
			logger.info("Verify adding non-existing address succeeded");
		}
		/**
		 * Try add and cancel location
		 * And find this location again
		 * But still can not find it
		 */
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		
		EventBasicType.Location location2 = new EventBasicType.Location();
		location2.setLocationName(locationName);
		location2.setAddress("address line 1");
		location2.setAddressContinue("address line 2");
		location2.setCityRegion("Shanghai");
		location2.setStateProvince("SH");
		location2.setPostalCode("200032");
		location2.setCounty("AG");
		location2.setPrivate(true);
		EventBasicType eventBasic = new EventBasicType();
		eventBasic.setLocation(location2);
		
		eventBasicSubview.setEventLocation(eventBasic);
		eventBasicSubview = eventBasicSubview.cancelLocation();
		
		// Create another event
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		try{
			eventBasicSubview = eventBasicSubview.selectEventLocationAJAX(location2);
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics005.class.getName(), 
					"Verify cancelling adding address", 
					"New address is still saved after cancelling"));
		}catch(RuntimeException e){
			logger.info("Verify cancelling adding address succeeded");
		}
		
		/**
		 * Try add and save location
		 * This time find the existing location and found
		 */
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		eventBasicSubview.setEventLocation(eventBasic);
		eventBasicSubview = eventBasicSubview.saveLocation();
		
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		try{
			eventBasicSubview = eventBasicSubview.selectEventLocationAJAX(location2);
			logger.info("Verify saving and find the added address succeeded");
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics005.class.getName(), 
					"Verify saving and find the added address", 
					"Saved address can not be found again"));
		}
		// logout
		loginBusiness.logout();
		
		// Popup failures
		FailureAssertion.pop(Basics005.class);
	}
	
}
